{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: BooleanConcept Extraction with References and Justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: BooleanConcept Extraction with References and Justifications\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import BooleanConcept, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Sample document text containing policy information\n",
        "policy_text = \"\"\"\n",
        "Company Data Retention Policy (Updated 2024)\n",
        "\n",
        "All customer data must be encrypted at rest and in transit using industry-standard encryption protocols.\n",
        "Personal information should be retained for no longer than 3 years after the customer relationship ends.\n",
        "Employees are required to complete data privacy training annually.\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document from the text\n",
        "doc = Document(raw_text=policy_text)\n",
        "\n",
        "# Create a BooleanConcept with justifications and references enabled\n",
        "compliance_concept = BooleanConcept(\n",
        "    name=\"Has encryption requirement\",\n",
        "    description=\"Whether the document specifies that data must be encrypted\",\n",
        "    add_justifications=True,  # Enable justifications to understand reasoning\n",
        "    justification_depth=\"brief\",\n",
        "    justification_max_sents=1,  # Allow up to 1 sentences for each justification\n",
        "    add_references=True,  # Include references to source text\n",
        "    reference_depth=\"sentences\",  # Reference specific sentences rather than paragraphs\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([compliance_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4o-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept\n",
        "compliance_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the extracted value with justification and references\n",
        "print(f\"Has encryption requirement: {compliance_concept.extracted_items[0].value}\")\n",
        "print(f\"\\nJustification: {compliance_concept.extracted_items[0].justification}\")\n",
        "print(\"\\nSource references:\")\n",
        "for sent in compliance_concept.extracted_items[0].reference_sentences:\n",
        "    print(f\"- {sent.raw_text}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}